﻿using BCCommon;
using BCData.OA.Notification.NotificationCategory;
using BCDto.OA.Notification.NotificationCategory;
using BCEntity.OA.Notification.NotificationCategory;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.Notification.NotificationCategory
{
    public class NotificationCategoryService : INotificationCategoryService
    {
        private readonly INotificationCategoryData notificationCategoryData;

        public NotificationCategoryService(INotificationCategoryData notificationCategoryData)
        {
            this.notificationCategoryData = notificationCategoryData;
        }

        public bool Exists(long notificationCategoryId)
        {
            return this.notificationCategoryData.Exists(notificationCategoryId).GetAwaiter().GetResult();
        }
        public bool Exists(string notificationCategoryName)
        {
            return this.notificationCategoryData.Exists(notificationCategoryName).GetAwaiter().GetResult();
        }

        public NotificationCategoryDto Add(NotificationCategoryRequestDto requestDto)
        {
            if (this.notificationCategoryData.Exists(requestDto.NotificationCategoryCode).GetAwaiter().GetResult())
            {
                throw new ArgumentException("通知分类编码已存在");
            }
            var entity = requestDto.As<NotificationCategoryEntity>();

            return this.notificationCategoryData.Add(entity).GetAwaiter().GetResult().As<NotificationCategoryDto>();
        }
        public NotificationCategoryDto Update(NotificationCategoryPutDto putDto)
        {
            if (!this.Exists(putDto.NotificationCategoryId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var oldEntity = this.notificationCategoryData.GetEntity(putDto.NotificationCategoryId).GetAwaiter().GetResult();
            if (oldEntity.NotificationCategoryCode != putDto.NotificationCategoryCode)
            {
                if (this.notificationCategoryData.Exists(putDto.NotificationCategoryCode).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("通知分类编码已存在");
                }
            }
            if (oldEntity.IsPredefined)
            {
                throw new ArgumentException("预定义分类无法修改");
            }
            var entity = putDto.As<NotificationCategoryEntity>();

            return this.notificationCategoryData.Update(entity).GetAwaiter().GetResult().As<NotificationCategoryDto>();
        }

        public bool Delete(long notificationCategoryId)
        {
            if (!this.Exists(notificationCategoryId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.notificationCategoryData.Delete(notificationCategoryId).GetAwaiter().GetResult();
        }

        public NotificationCategoryDto GetEntity(long notificationCategoryId)
        {
            return this.notificationCategoryData.GetEntity(notificationCategoryId).GetAwaiter().GetResult().As<NotificationCategoryDto>();
        }

        public IEnumerable<NotificationCategoryDto> GetList()
        {
            return this.notificationCategoryData.GetList().GetAwaiter().GetResult().As<IEnumerable<NotificationCategoryDto>>();
        }

        public PageableList<NotificationCategoryDto> Query(string notificationCategoryName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.notificationCategoryData.Query(notificationCategoryName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<NotificationCategoryDto>
            {
                Items = list.As<IEnumerable<NotificationCategoryDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
